//
//  CustomCalloutView.m
//  HelloAmap
//
//  Created by xiaoming han on 14-10-21.
//  Copyright (c) 2014年 AutoNavi. All rights reserved.
//

#import "CustomCalloutView.h"
#import <QuartzCore/QuartzCore.h>
#import "homeViewController.h"
#import "HomeNextViewController.h"
#import "UIImage+orghtimage.h"

#define kArrorHeight        10

#define kPortraitMargin     5
#define kPortraitWidth      70
#define kPortraitHeight     50

#define kTitleWidth         40
#define kTitleHeight        40


@interface CustomCalloutView (){

    NSString *_annotationTitle;

}

@property (nonatomic, strong) UIImageView *portraitView;
@property (nonatomic, strong) UILabel *subtitleLabel;
@property (nonatomic, strong) UILabel *titleLabel;
@property(nonatomic,strong)UILabel*PMlabel;
@property(nonatomic,strong)UILabel*Airlabel;
@property(nonatomic,strong)UILabel*Business;
@property(nonatomic,strong)UIImageView*discount;
@end

@implementation CustomCalloutView

- (id)initWithFrame:(CGRect)frame title:(NSString *)title
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.frame=CGRectMake(0, 0, 240, 120);
        self.backgroundColor = [UIColor clearColor];
        [self initSubViews:title];
    }
    return self;
}

- (void)initSubViews:(NSString *)title
{
    _annotationTitle = title;
//    // 添加图片
    self.portraitView = [[UIImageView alloc] initWithFrame:CGRectMake(kPortraitMargin  + kTitleWidth-40, 5, 100, 100)];

    self.portraitView.image=[UIImage imageNamed:@"气泡小图.png"];
    
    
    [self addSubview:self.portraitView];
    
    // 商场标题
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(110, 62, 125, 30)];

    [self.titleLabel setFont:[UIFont boldSystemFontOfSize:15]];;
     self.titleLabel.numberOfLines = 0;
     self.titleLabel.text = @"titletitletitletitle";
    [self addSubview:self.titleLabel];
     self.titleLabel.textAlignment=NSTextAlignmentLeft;
    
    
    //PM2.5 label
    UILabel*label=[[UILabel alloc]initWithFrame:CGRectMake(110, 10, 50, 10)];
    [label setFont:[UIFont boldSystemFontOfSize:14]];
    label.numberOfLines = 0;
    label.text=@"PM2.5:";
    [self addSubview:label];
    
    //空气指数label
    UILabel*label2=[[UILabel alloc]initWithFrame:CGRectMake(110, 30, 80, 10)];
    [label2 setFont:[UIFont boldSystemFontOfSize:14]];
     label2.numberOfLines = 0;
     label2.text=@"空气指数:";
     [self addSubview:label2];
    
    //合作商家label
    self.Bussinslabel=[[UILabel alloc]initWithFrame:CGRectMake(110, 50, 80, 10)];
    [ self.Bussinslabel setFont:[UIFont boldSystemFontOfSize:14]];
    //    label2.backgroundColor=[UIColor greenColor];
     self.Bussinslabel.numberOfLines = 0;
     self.Bussinslabel.text=@"合作商家:";
    [self addSubview: self.Bussinslabel];
    
    //PM值
    self.PMlabel=[[UILabel alloc]initWithFrame:CGRectMake(180, 10, 50, 10)];
    [self.PMlabel setFont:[UIFont boldSystemFontOfSize:14]];
     self.PMlabel.text=@"10";
     self.PMlabel.textColor=[UIColor redColor];
    [self addSubview:self.PMlabel];
    
    
    //空气指数值
    self.Airlabel=[[UILabel alloc]initWithFrame:CGRectMake(181, 30, 80, 10)];
    [self.Airlabel setFont:[UIFont boldSystemFontOfSize:14]];
     self.Airlabel.text=@"优";
    self.Airlabel.textColor=[UIColor greenColor];
    [self addSubview:self.Airlabel];
    
    
    
    //合作商家数量
    
    self.Business=[[UILabel alloc]initWithFrame:CGRectMake(180, 50, 80, 10)];
    
    [self.Business setFont:[UIFont boldSystemFontOfSize:14]];
     self.Business.text=@"2000家";
    self.Business.textColor=[UIColor blueColor];
    [self addSubview:self.Business];
    
    
    //折扣图片
    self.discount=[[UIImageView alloc]initWithFrame:CGRectMake(111, 90, 90, 12)];
    self.discount.backgroundColor=[UIColor yellowColor];
    
    self.discount.image=[UIImage imageNamed:@"敬请期待.png"];
    [self addSubview:self.discount];
    
    
    
    
    
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(ID_huiDiao)];
    [self addGestureRecognizer:tap];
}


#pragma mark - Override

- (void)setTitle:(NSString *)title
{
    self.titleLabel.text = title;
}

- (void)setSubtitle:(NSString *)subtitle
{
    self.subtitleLabel.text = subtitle;
}

- (void)setImage:(UIImage *)image
{
    self.portraitView.image = image;
}

-(void)setPMzhi:(NSString *)PMzhi{

    self.PMlabel.text=PMzhi;
}

-(void)setAirzhi:(NSString *)Airzhi{

    self.Airlabel.text=Airzhi;
}

-(void)setBusinesszhi:(NSString *)Businesszhi{

    self.Business.text=Businesszhi;
}

-(void)setDiscountzhi:(UIImage *)discountzhi{
   
    self.discount.image=discountzhi;
}

- (void)setDiscountzhiURL:(NSString *)discountzhiURL{
    if (discountzhiURL != nil) {
            
        [self.discount sd_setImageWithURL:[NSURL URLWithString:discountzhiURL] placeholderImage:[UIImage imageNamed:@"敬请期待.png"]];
      
        _discountzhiURL = discountzhiURL;
    }
}

- (void)setImageURL:(NSString *)imageURL{
    if (imageURL != nil) {
            
        [self.portraitView sd_setImageWithURL:[NSURL URLWithString:imageURL] placeholderImage:[UIImage imageNamed:@"气泡小图.png"]];
        
        _imageURL = imageURL;
    }
}







#pragma mark - draw rect

- (void)drawRect:(CGRect)rect
{
    
    [self drawInContext:UIGraphicsGetCurrentContext()];
    
    self.layer.shadowColor = [[UIColor blackColor] CGColor];//阴影颜色
    self.layer.shadowOpacity = 1.0; // 阴影度
    self.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);  //阴影层
    
}

- (void)drawInContext:(CGContextRef)context
{
    
    CGContextSetLineWidth(context, 2.0);
    CGContextSetFillColorWithColor(context, [UIColor colorWithRed:254/255.0 green:254/255.0  blue:254/255.0  alpha:1].CGColor);
    
    [self getDrawPath:context];
    CGContextFillPath(context);
    
}

- (void)getDrawPath:(CGContextRef)context

{
    CGRect rrect = self.bounds;
    CGFloat radius = 9.0;
    CGFloat minx = CGRectGetMinX(rrect),
    midx = CGRectGetMidX(rrect)+30,  //箭头位置
    maxx = CGRectGetMaxX(rrect);
    CGFloat miny = CGRectGetMinY(rrect),
    maxy = CGRectGetMaxY(rrect)-kArrorHeight;
    
    CGContextMoveToPoint(context, midx+kArrorHeight-30, maxy);
    CGContextAddLineToPoint(context,midx-30, maxy+kArrorHeight);
    CGContextAddLineToPoint(context,midx-kArrorHeight-20-10, maxy);
    
    CGContextAddArcToPoint(context, minx, maxy, minx, miny, radius);
    CGContextAddArcToPoint(context, minx, minx, maxx, miny, radius);
    CGContextAddArcToPoint(context, maxx, miny, maxx, maxx, radius);
    CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius);
    CGContextClosePath(context);
}

//点击view 跳转的方法
-(void)ID_huiDiao{
  
    [[NSNotificationCenter defaultCenter] postNotificationName:@"next" object:nil userInfo:@{@"title":_annotationTitle}];
  

}

@end
